package com.hsy.sunflowerbao.login.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.flux.action.RxStoreChange
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.ui.activity.BaseActivityWithToolbar
import com.hsy.sunflowerbao.common.ui.MainActivity
import com.hsy.sunflowerbao.login.action.LoginActionCreator
import com.hsy.sunflowerbao.login.store.ShopsStore
import com.hsy.sunflowerbao.utils.CommonUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.layout_refresh_recycle.*

/**
 * 选择店铺页面
 * Created on 2018/3/27.
 *
 * @author Yang
 */
class ShopsActivity : BaseActivityWithToolbar<ShopsStore>(), OnRefreshListener {

    /**
     * 事件产生器
     */
    private val creator = LoginActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = ShopsStore(dispatcher)
    /**
     * 专家库适配器
     */
    private lateinit var adapter: ShopsAdapter
    /**
     * 请求失败页
     */
    private var failureView: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_refresh_recycle)
        setTitle(R.string.title_shops)

        adapter = ShopsAdapter(this, listOf(), -1)

        refresh.setOnRefreshListener(this)
        refresh.isEnableLoadMore = false
        recycle.layoutManager = LinearLayoutManager(recycle.context)
        recycle.adapter = adapter

        CommonUtils.showProgressDialog(this)
        onRefresh(refresh)
    }

    /**
     * 数据刷新响应
     * @param change 需要响应的改变
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        super.onRxStoreChanged(change)
        if (change.storeId == store.tag) when (change.type) {
            ActionType.GET_SHOPS -> {
                CommonUtils.hideProgressDialog()
                refresh.finishRefresh()
                if (change.success) {
                    if (failureView != null) {
                        homeParent.removeView(failureView)
                        failureView = null
                    }
                    adapter.items = store.shops
                    adapter.notifyDataSetChanged()
                } else {
                    failureView = CommonUtils.showNetworkFailure(this, homeParent, View.OnClickListener { _ ->
                        CommonUtils.showProgressDialog(this)
                        onRefresh(refresh)
                    })
                }
            }
            ActionType.SELECT_SHOP -> {
                CommonUtils.saveSid(store.shop.sid)
                CommonUtils.saveShopName(store.shop.shop_name)
                CommonUtils.hideProgressDialog()
                startActivityWithTaskCleared(MainActivity::class.java)
            }
        }
    }

    fun selectShop(sid: String) {
        CommonUtils.showProgressDialog(this)
        creator.selectShop(sid)
    }

    /**
     * 重载刷新事件
     */
    override fun onRefresh(refresh: RefreshLayout) {
        creator.getShops()
    }

    override fun onBackPressed() {
        CommonUtils.clearToken()
        super.onBackPressed()
    }
}